package com.cloud.cloudcommons;

import com.cloud.cloudcommons.md5.MD5;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.StringValueResolver;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class CloudCommonsApplication implements CommandLineRunner, EmbeddedValueResolverAware {

    StringValueResolver valueResover;
    String key;
    String lightText;

    public static void main(String[] args) {
        SpringApplication.run(CloudCommonsApplication.class, args);
    }


    @RequestMapping("/hi")
    public String hi(HttpServletRequest request) throws Exception {
        Object attribute = request.getHeader("Can-Excutor");
        if(attribute!=null){
            String canSkill = (String) attribute;
            boolean verify = MD5.verify(lightText, key, canSkill);
            if(verify)
                System.out.println("拥有令牌可以参与抢购...");
            else
                System.out.println("没有令牌，无法参与抢购.....~~");
        }
        return  ("hi i am port : " + valueResover.resolveStringValue("${server.port}"));
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("CloudCommonsApplication....initlaztion...."+args);
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        this.valueResover = stringValueResolver;
        this.key = valueResover.resolveStringValue("${skillGoods.md5.key}");
        this.lightText = valueResover.resolveStringValue("${skillGoods.md5.lightText}");
    }
}
